package com.xingchen.springboot3.controller;

import com.github.pagehelper.PageInfo;
import com.xingchen.springboot3.common.bean.PageBean;
import com.xingchen.springboot3.common.bean.Result;
import com.xingchen.springboot3.pojo.dto.CategoryDto;
import com.xingchen.springboot3.pojo.entity.Category;
import com.xingchen.springboot3.service.CategroyService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;


/**
 *
 * @author weijingchen
 * @DateTime 2025-09-26
 */
@RestController
@RequestMapping("/categroy")
@RequiredArgsConstructor
public class CategroyController {
    private final CategroyService categroyService;

    @PostMapping("/add")
    public Result add(@Validated(CategoryDto.add.class) @RequestBody CategoryDto dto) {
        categroyService.add(dto);
        return Result.success();
    }

    @PostMapping("/update")
    public Result update(@Validated(CategoryDto.update.class) @RequestBody CategoryDto dto) {
        categroyService.update(dto);
        return Result.success();
    }

    @DeleteMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        categroyService.delete(id);
        return Result.success();
    }

    @PostMapping("/list")
    public Result<PageBean<Category>> list(@RequestBody CategoryDto dto) {
        return Result.success(categroyService.list(dto));
    }

    @GetMapping("/id")
    public Result<Category> getById(@RequestParam Integer id) {
        return Result.success(categroyService.getById(id));
    }
}
